package com.ds.infrastructure.hyperspace.console.controller;

import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbStage;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.model.request.form.PageForm;
import com.ds.infrastructure.hyperspace.console.model.request.stage.ListStageForm;
import com.ds.infrastructure.hyperspace.console.model.request.stage.SaveStageForm;
import com.ds.infrastructure.hyperspace.console.model.request.stage.UpdateStageForm;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResult;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultData;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultList;
import com.ds.infrastructure.hyperspace.console.model.response.result.JsonResultOk;
import com.ds.infrastructure.hyperspace.console.model.response.stage.GetStageVO;
import com.ds.infrastructure.hyperspace.console.model.response.stage.ListStageVO;
import com.ds.infrastructure.hyperspace.console.service.inf.IEnvironmentService;
import com.ds.infrastructure.hyperspace.console.service.inf.IStageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author: yaozhendong
 * @create: 2019-09-20 09:36
 **/

@RestController
@RequestMapping(value = "/stage", produces = {"application/json;charset=UTF-8"})
@Api(value = "Manager Stage Controller", tags = "阶段管理")
public class StageController {
    @Autowired
    IStageService iStageService;
    @Autowired
    IEnvironmentService iEnvironmentService;

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存阶段")
    public JsonResultData<Long> save(@RequestBody @Valid SaveStageForm form) {
        iEnvironmentService.getOrThrow(form.getEnvironmentId());
        TbStage item = iStageService.selectOne(SaveStageForm.toEntity4SelectOne(form), null);
        if (item != null) {
            throw new HyperspaceException("保存阶段失败，阶段名称已经存在，stageName=" + form.getStageName());
        }
        TbStage entity = SaveStageForm.toEntity(form);
        iStageService.save(entity);
        return JsonResultData.build(entity.getId());
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改阶段")
    public JsonResult update(@RequestBody @Valid UpdateStageForm form) {
        TbStage item = iStageService.get(form.getId());
        if (item == null) {
            throw new HyperspaceException("修改阶段失败，没有找到对应的记录;当前id=" + form.getId());
        }
        TbStage item4Check = iStageService.selectOne(UpdateStageForm.toEntity4SelectOne(form, item.getEnvironmentId()), null);
        if (item4Check != null && !form.getId().equals(item4Check.getId())) {
            throw new HyperspaceException("修改阶段失败，阶段名称已经存在，stageName=" + form.getStageName());
        }
        int count = iStageService.update(UpdateStageForm.toEntity(form));
        if (count == 0) {
            throw new HyperspaceException("修改阶段失败，没有找到对应的记录;当前id=" + form.getId());
        }
        return JsonResultOk.SUCCESS;
    }


    @PostMapping(value = "/list")
    @ApiOperation(value = "阶段列表")
    public JsonResultList<ListStageVO> list(@RequestBody @Valid ListStageForm form) {
        TbStage where = ListStageForm.toEntity(form);
        TbPageWhere pageWhere = PageForm.toPageWhere(form);
        int dataTotal = iStageService.getTotal(where, null);
        if (dataTotal == 0 || pageWhere.getBegin() > dataTotal) {
            return JsonResultList.build(Collections.emptyList(), dataTotal);
        }
        List<TbStage> tbStageList = iStageService.getList(where, null, pageWhere);
        List<Long> environmentIdList = tbStageList.stream().map(TbStage::getEnvironmentId).distinct().collect(Collectors.toList());
        Map<Long, String> tbEnvironmentMap = iEnvironmentService.getMap(environmentIdList);
        List<ListStageVO> dataList = new ArrayList<>();
        tbStageList.forEach(item -> dataList.add(ListStageVO.toVO(item, tbEnvironmentMap)));
        return JsonResultList.build(dataList, dataTotal);
    }

    @GetMapping(value = "/get")
    @ApiOperation(value = "查看阶段")
    public JsonResultData<GetStageVO> get(@RequestParam(name = "id") Long id) {
        TbStage entity = iStageService.getOrThrow(id);
        GetStageVO result = GetStageVO.toVO(entity);
        return JsonResultData.build(result);
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation(value = "删除-阶段")
    public JsonResult delete(@RequestParam(name = "id") Long id) {
        iStageService.getOrThrow(id, "删除阶段失败");
        iStageService.delete(id);
        return JsonResultOk.SUCCESS;
    }
}
